<template>
    <div class="app-container">
        <el-row :gutter="20">
            <el-col :span="24" :xs="24">
                <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
                    <el-form-item :label="$locale('geWudevice.Equipment name')" prop="deviceName">
                        <el-input v-model="queryParams.deviceName" :placeholder="$locale('geWudevice.Please enter the device name')" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item :label="$locale('geWudevice.add.state')" prop="status">
                        <el-select v-model="queryParams.status" :placeholder="$locale('geWudevice.add.state')" clearable style="width: 240px">
                            <el-option v-for="dict in dict.type.experimental_status" :key="dict.value" :label="dict.label" :value="+dict.value" />
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $locale('geWudevice.add.search for') }}</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>

                <!-- <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
                    </el-col>
                </el-row> -->

                <el-table v-loading="loading" :data="deviceList">
                    <el-table-column type="index" width="50" :label="$locale('geWudevice.add.Serial Number')" />
                    <el-table-column :label="$locale('geWudevice.Equipment number')" align="center" key="deviceNum" prop="deviceNum" />
                    <el-table-column :label="$locale('geWudevice.Equipment name')" align="center" key="deviceName" prop="deviceName" width="160">
                    </el-table-column>
                    <el-table-column :label="$locale('geWudevice.add.state')" align="center" key="status">
                        <template slot-scope="scope">
                            <span v-for="item in dict.type.experimental_status" :key="item.value">{{ item.value == scope.row.status?item.label:'' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column :label="$locale('geWudevice.founder')" align="center" key="createBy" prop="createBy" />
                    <el-table-column :label="$locale('geWudevice.Creation time')" align="center" prop="createTime" width="160">
                    </el-table-column>
                    <el-table-column :label="$locale('geWudevice.add.describe')" align="center" key="description" prop="description">
                        <template slot-scope="scope">
                            <span v-html="scope.row.description"></span>
                        </template>
                    </el-table-column>
                    <el-table-column :label="$locale('formMarkering.list.operate')" align="center" width="160" class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" @click="handleUpdate(scope.row,true)">{{ $locale('geWudevice.view') }}</el-button>
                            <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button> -->
                            <el-button size="mini" type="text" @click="handleShopping(scope.row)">{{ $locale('geWudevice.Shopping Cart') }}</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
            </el-col>
        </el-row>

        <!-- 添加 -->
        <addDeviceList ref="addDeviceList" @getList="getList"></addDeviceList>
        <el-dialog :title="$locale('geWudevice.Shopping Cart')" :visible.sync="open" width="500px">
            <el-table :data="listData" :max-height="400">
                <el-table-column type="index" width="50" :label="$locale('geWudevice.add.Serial Number')" />
                <el-table-column :label="$locale('geWudevice.Shopping cart number')" align="center" key="experimentType" prop="experimentType" />
                <el-table-column :label="$locale('geWudevice.code')" align="center" key="status" prop="status" />
                <el-table-column :label="$locale('geWudevice.quantity')" align="center" key="difficultyLevel" prop="difficultyLevel" />
            </el-table>
        </el-dialog>
    </div>
</template>

<script>
import { deleteById, list } from "@/api/geWudevice";
import { addByDevice } from "@/api/myOrder/shoppingList";
import addDeviceList from './add/index.vue'
import { getNames } from '@/utils/index'

// 格悟设备
export default {
    name: "experiment",
    dicts: ['experimental_status', 'difficulty_level', 'experiment_time', 'experiment_type', 'whether_not'],
    components: { addDeviceList },
    data () {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 用户表格数据
            deviceList: [],
            listData: [],
            // 弹出层标题
            title: "",
            // 部门树选项
            deptOptions: undefined,
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                deviceName:'',
                status:''
            },
        };
    },
    created () {
        this.getList();
    },
    methods: {
        /** 查询用户列表 */
        getList () {
            this.loading = true;
            list(this.queryParams).then(({ data }) => {
                this.deviceList = data.records;
                this.total = data.total;
                this.loading = false;
            });
        },
        /** 搜索按钮操作 */
        handleQuery () {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery () {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd () {
            this.$refs.addDeviceList.handleAdd()
        },
        /** 修改按钮操作 */
        handleUpdate (row, disabled) {
            this.$refs.addDeviceList.handleUpdate(row, disabled)
        },
        /** 删除按钮操作 */
        handleDelete (row) {
            const userIds = row.id || this.ids;
            this.$modal.confirm(this.$locale('geWudevice.tip1') + getNames(this.deviceList, userIds, 'deviceName') + this.$locale('geWudevice.tip2')).then(function () {
                return deleteById({ id: userIds });
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess(this.$locale('geWudevice.tip3'));
            }).catch(() => { });

        },
        /** 购物车按钮操作 */
        handleShopping (row) {
            this.$modal.confirm(this.$locale('geWudevice.tip4') + getNames(this.deviceList, row.id, 'deviceName') + this.$locale('geWudevice.tip2')).then(function () {
                return addByDevice(row.id)
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess(this.$locale('geWudevice.tip5'));
            }).catch(() => { });
        },
    }
};
  </script>
